package graph

type Bag struct {
	first *node
	size  int
}

type node struct {
	Item interface{}
	next *node
}

func NewBag() *Bag {
	return &Bag{
		first: nil,
		size:  0,
	}
}

func (b *Bag) IsEmpty() bool {
	return b.first == nil
}

func (b *Bag) iterator() Iterator {
	return &ListIterator{
		current: b.first,
	}
}

func (b *Bag) Add(val interface{}) {
	oldFirst := b.first
	b.first = &node{
		Item: val,
		next: oldFirst,
	}
	b.size++
}

type ListIterator struct {
	current *node
}

func (l *ListIterator) HasNext() bool {
	return l.current != nil
}

func (l *ListIterator) Next() interface{} {
	if !l.HasNext() {
		panic("no such element")
	}
	item := l.current.Item
	l.current = l.current.next
	return item
}
